package com.metagurukul.informer.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.informer.service.SearchService;
import com.metagurukul.informer.valueObjects.Questions;

/**
 * Servlet implementation class Search
 */
public class Search extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Search() {
        super();
     
    }
    protected void doPerform(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			
			String searchString=request.getParameter("query");
			if(searchString==null){
				response.sendRedirect("Home.jsp");
			}
			SearchService searchservice=new SearchService(searchString);
			List<Questions> questions= searchservice.questionList();
			HttpSession session=request.getSession();
			session.setAttribute("pageName", "search");
			session.setAttribute("searchString", searchString);
			session.setAttribute("listOfQuestions", questions);
			request.getRequestDispatcher("RetrieveQuestion.jsp").forward(request, response);
		} catch (SQLException e) {
			request.getRequestDispatcher("Error.jsp").forward(request, response);
		}
		
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("AskQuestion.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPerform(request,response);
	}

}
